import {
  checkCaptcha,
  checkPhoneCode,
  getCaptcha,
  updatePassword,
  getInfo,
  getPhoneCode,
  login,
  logout,
  lookAllUser,
  updateUser,
  updatePhone,
  uploadAvatar,
  register,
} from '@/api/user'
import { getToken, removeToken, setToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    id: '',
    role: '',
    userInfo: {},
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ID: (state, id) => {
    state.id = id
  },
  SET_ROLE: (state, role) => {
    state.role = role
  },
  SET_INFO: (state, user) => {
    state.userInfo = user
  },
}

const actions = {
  // user login
  async login({ commit }, userInfo) {
    const { phone, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ phone: phone.trim(), password: password })
        .then((response) => {
          const { data } = response
          commit('SET_TOKEN', data)
          setToken(data)
          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  // 条件查询
  async lookUser({ commit }, user) {
    return new Promise((resolve, reject) => {
      lookAllUser(user)
        .then((response) => {
          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 修改用户
  async updateUser({ commit }, user) {
    return new Promise((resolve, reject) => {
      updateUser(user)
        .then((response) => {
          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 头像上传
  async uploadAvatar({ commit }, { uid, fid }) {
    return new Promise((resolve, reject) => {
      uploadAvatar(uid, fid)
        .then((response) => {
          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 获取验证码
  async getPhoneCode({ commit }, phone) {
    return new Promise((resolve, reject) => {
      getPhoneCode(phone)
        .then((response) => {
          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 修改密码
  async updatePassword({ commit }, obj) {
    return new Promise((resolve, reject) => {
      updatePassword(obj)
        .then((response) => {
          removeToken() // must remove  token  first
          resetRouter()
          commit('RESET_STATE')
          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 校验验证码
  async checkPhoneCode({ commit }, { phone, code }) {
    return new Promise((resolve, reject) => {
      checkPhoneCode(phone, code)
        .then((response) => {
          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 修改手机号
  async updatePhone({ commit }, data) {
    return new Promise((resolve, reject) => {
      updatePhone(data)
        .then((response) => {
          removeToken() // must remove  token  first
          resetRouter()
          commit('RESET_STATE')
          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 获取用户信息
  async getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token)
        .then((response) => {
          const { data, success } = response
          if (!success) {
            return reject('验证失败!请重新登录!')
          }
          const { id, username, avatar, role } = data
          commit('SET_ID', id)
          commit('SET_NAME', username)
          commit('SET_AVATAR', avatar)
          commit('SET_ROLE', role)
          commit('SET_INFO', data)
          resolve(data)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 注册
  async register({ commit, state }, user) {
    return new Promise((resolve, reject) => {
      register(user)
        .then((response) => {
          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 退出登录
  async logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token)
        .then((response) => {
          removeToken() // must remove  token  first
          resetRouter()
          commit('RESET_STATE')

          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },

  // 删除token
  async resetToken({ commit }) {
    return new Promise((resolve) => {
      removeToken()
      commit('RESET_STATE')
      resolve()
    })
  },
  // 获取验证码
  async getCaptcha({ state }, type) {
    return new Promise((resolve, reject) => {
      getCaptcha(type)
        .then((response) => {
          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  // 获取验证码
  async checkCaptcha({ state }, { uid, code }) {
    return new Promise((resolve, reject) => {
      checkCaptcha(uid, code)
        .then((response) => {
          resolve(response)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
}
